#!/bin/bash


#DIR="tmp/20240927/dark_and_bias"
DIR="tmp/20240928/dome_flat_tel_power_on"

if [ -d ${DIR} ]; then
    echo "> ${DIR} already exist."
else
    mkdir -p ${DIR}
fi

LOG_FILE="${DIR}/auto.log"

if [ -f ${LOG_FILE} ];then
    echo "--> ${LOG_FILE} exits."
else
    touch ${LOG_FILE}
fi

echo "# START of LOG" >> ${LOG_FILE}
echo $(date) >> ${LOG_FILE}

# 曝光命令
CMD_EXPOSURE='./exposure'

# 保存DDR中数据到本地（行数、列数需要与CCD读出时序相匹配）
N_ROW=4700
N_COL=1250
N_BYTES=2
FAST_MODE=0



### bias
./refresh
n=1
while [ ${n} -le 10 ]
do
    FOUT_FNAME=${DIR}/bias_${n}.bin
    bin/ccd290_exposure -r ${N_ROW} -c ${N_COL} -f ${FAST_MODE} -t 0 -o ${FOUT_FNAME}
    echo `date`"  ${FOUT_FNAME}" >> ${LOG_FILE}
	n=$[ ${n} + 1 ]
done

### flat
expT=1
expT_MAX=9
while [ ${expT} -le ${expT_MAX} ]
do
    ./refresh
    FOUT_FNAME=${DIR}/flat_${expT}s_1.bin
    bin/ccd290_exposure -r ${N_ROW} -c ${N_COL} -f ${FAST_MODE} -t ${expT} -o ${FOUT_FNAME}
    echo `date`"  ${FOUT_FNAME}" >> ${LOG_FILE}

    ./refresh
    FOUT_FNAME=${DIR}/flat_${expT}s_2.bin
    bin/ccd290_exposure -r ${N_ROW} -c ${N_COL} -f ${FAST_MODE} -t ${expT} -o ${FOUT_FNAME}
    echo `date`"  ${FOUT_FNAME}" >> ${LOG_FILE}

	expT=$[ ${expT} + 1 ]
done


### flat
expT=10
expT_MAX=120
while [ ${expT} -le ${expT_MAX} ]
do
    ./refresh
    FOUT_FNAME=${DIR}/flat_${expT}s_1.bin
    bin/ccd290_exposure -r ${N_ROW} -c ${N_COL} -f ${FAST_MODE} -t ${expT} -o ${FOUT_FNAME}
    echo `date`"  ${FOUT_FNAME}" >> ${LOG_FILE}

    ./refresh
    FOUT_FNAME=${DIR}/flat_${expT}s_2.bin
    bin/ccd290_exposure -r ${N_ROW} -c ${N_COL} -f ${FAST_MODE} -t ${expT} -o ${FOUT_FNAME}
    echo `date`"  ${FOUT_FNAME}" >> ${LOG_FILE}

	expT=$[ ${expT} + 10 ]
done

echo "# END of BIAS-FIELD LOG" >> ${LOG_FILE}

